package eerbaar.action.internal.lib.position;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import eerbaar.area.IArea;
import eerbaar.elements.IElement;
import eerbaar.game.IGame;
import eerbaar.position.IPosition;
import eerbaar.position.PositionFactory;

public class RandomPositionGenerator implements IPositionGenerator {

    @Override
    public IPosition next(IGame game) {

        IArea area = game.getArea();
        List<IPosition> list = new ArrayList<IPosition>();
        for (int x = 0; x < area.getWidth(); x++) {
            for (int y = 0; y < area.getHeight(); y++) {
                IPosition p = PositionFactory.createPosition(x, y);
                list.add(p);
            }
        }
        IElement[] es = area.getElements();
        for (IElement e : es) {
            list.remove(e.getPosition());
        }

        if (list.size() == 0) {
            throw new RuntimeException("No space in area");
        }

        Date date = new Date();
        long time = date.getTime();
        Random random = new Random(time);
        random = new Random();

        int index = random.nextInt(list.size());
        return list.get(index);
    }
}
